package com.ebo.upload;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.List;

import io.agora.openvcall.R;
import utils.Lg;

import static com.ebo.upload.UploadListActivity.list_Upload;

/**
 * Created by admin on 2017/6/6.
 */

public class UploadAdaper extends BaseAdapter {
    private LayoutInflater inflater;//这个一定要懂它的用法及作用
//    List<MultipartUpload> list_Upload;

    public UploadAdaper(Context context, List<MultipartUpload> _list_Upload) {
        this.inflater = LayoutInflater.from(context);
//        list_Upload = _list_Upload;
    }

    @Override
    public int getCount() {
        return list_Upload.size();
    }

    @Override
    public Object getItem(int position) {
        return list_Upload.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

//        if (convertView == null) {
            holder = new ViewHolder();

            //把vlist layout转换成View【LayoutInflater的作用】
            convertView = inflater.inflate(R.layout.item_upload, null);
            //通过上面layout得到的view来获取里面的具体控件


            holder.tv_file_name= (TextView) convertView.findViewById(R.id.tv_file_name);
            holder.tv_file_size= (TextView) convertView.findViewById(R.id.tv_file_size);
            holder.btn_left= (Button) convertView.findViewById(R.id.btn_left);
            holder.btn_Right= (Button) convertView.findViewById(R.id.btn_right);
            holder. progress_bar =(ProgressBar)convertView.findViewById(R.id.progress_bar);
            holder.tv_progress_number= (TextView) convertView.findViewById(R.id.tv_progress_number);

            convertView.setTag(holder);
//        } else {
//            holder = (ViewHolder) convertView.getTag();
//        }

        //文件名
        if(list_Upload==null){
            Lg.d(" list_Upload==null");
            return null;
        }
        if(list_Upload.get(position)==null){
            Lg.d(" list_Upload.get("+position+")==null");
            return null;
        }
        if(list_Upload.get(position).nameObject==null){
            Lg.d("list_Upload.get(position).nameObject");
            return null;
        }
        holder.tv_file_name.setText(list_Upload.get(position).nameObject);
        //文件大小
        holder.tv_file_size.setText(""+list_Upload.get(position).size_Max);
        //按钮
        holder.btn_left.setVisibility(View.VISIBLE);
        holder.btn_Right.setVisibility(View.VISIBLE);
        if(list_Upload.get(position).size_Finished < list_Upload.get(position).size_Max)
        {
            //下载未完成
            if(list_Upload.get(position).isPause())
            {
                //暂停中
                holder.btn_left.setBackgroundResource(R.drawable.upload_continue);
                holder.btn_Right.setBackgroundResource(R.drawable.upload_delete);
            }else{
                //下载中
                holder.btn_left.setBackgroundResource(R.drawable.upload_pause);
                holder.btn_Right.setBackgroundResource(R.drawable.upload_delete);
            }
        }else{
            //下载已完成
            holder.btn_left.setVisibility(View.INVISIBLE);
            holder.btn_Right.setBackgroundResource(R.drawable.upload_finish);
        }
        //进度条
        holder.progress_bar.setProgress( list_Upload.get(position).progress );
        //百分比
        holder.tv_progress_number.setText(list_Upload.get(position).progress+"%");

        return convertView;
    }






    public static class ViewHolder {
        TextView tv_file_name;
        TextView tv_file_size;
        Button btn_left;
        Button btn_Right;
        ProgressBar progress_bar;
        TextView tv_progress_number;
    }



}
